package cn.wendellup.bootStrap.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.rmi.RemoteException;
import java.security.NoSuchAlgorithmException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.util.Utils;
import cn.wendellup.bootStrap.service.contract.IFileService;
import cn.wendellup.bootStrap.valueobject.constants.ConstVar;
import cn.wendellup.bootStrap.valueobject.constants.FileUsedType;
import cn.wendellup.bootStrap.valueobject.entity.FileInfo;

@Component
public class Urls {
	private static Logger logger = Logger.getLogger(Urls.class);
    
//	@Autowired
//	private IFileService fileServiceImpl;
	
	public static Urls getInstance(){
		return new Urls();
	}
	
	/** user_center_url接口所返回的url  end */

	public static String getRequestUrl(HttpServletRequest req, HttpServletResponse resp,String url, String... args) {
		if (args == null) {
			return url;
		}
		for (int i = 0; i < args.length; i++) {
			if (i == 0) {
				url += "?" + args[i];
			} else {
				url += "&" + args[i];
			}
		}

		return url;
	}

	public String getFileUrlByEfsId(IFileService fileServiceImpl,
			long efsId, FileUsedType fileUsedTypeStore,
			FileUsedType fileUsedTypeShow){
		String fileUrl = null;
		try {
			FileInfo fileInfo = fileServiceImpl.getFileInfoByEfsId(efsId);
			if (fileInfo != null
					&& fileInfo.getFileUsedType().value() == fileUsedTypeStore
							.value()) {
				if(Utils.stringIsNullOrEmpty(fileInfo.getSaveName())){
					return fileUrl;
				}
				String fileName = getEncodeUrl(fileInfo.getSaveName());
				fileUrl = ConstVar.DOWNLOAD_URL
						+ FileUtils.getFilePath(fileUsedTypeShow,
								fileInfo.getEFSId(), fileName);
			}
		} catch (RemoteException e) {
			logger.error("", e);
		} catch (NoSuchAlgorithmException e) {
			logger.error("", e);
		} catch (UnsupportedEncodingException e) {
			logger.error("", e);
		}
		return fileUrl;
	}

	private static String getEncodeUrl(String fileName)
			throws UnsupportedEncodingException {
		fileName = URLEncoder.encode(fileName, "utf-8");
		if (fileName != null) {
			fileName = fileName.replace("+", "%20");
		}
		return fileName;
	}
	/**
	 * 
	 * @param appId
	 * @param loginUserId
	 * @param fileInfo
	 * @param fileUsedTypeStore
	 * @param fileUsedTypeShow
	 * @return
	 * @throws ExceptionCommonBase
	 */
	public String getFileUrlByFileInfo(
			FileInfo fileInfo, FileUsedType fileUsedTypeStore,
			FileUsedType fileUsedTypeShow) throws ExceptionCommonBase {
		String fileUrl = null;
		try {
			if (fileInfo != null
					&& fileInfo.getFileUsedType().value() == fileUsedTypeStore
							.value()) {
				if(Utils.stringIsNullOrEmpty(fileInfo.getSaveName())){
					return fileUrl;
				}
				String fileName = getEncodeUrl(fileInfo.getSaveName());
				fileUrl = ConstVar.DOWNLOAD_URL
						+ FileUtils.getFilePath(fileUsedTypeShow,
								fileInfo.getEFSId(), fileName);
			}
		} catch (RemoteException e) {
			logger.error(e);
			throw ExceptionCommonBase.throwExceptionCommonBase(e);
		} catch (NoSuchAlgorithmException e) {
			logger.error(e);
			throw ExceptionCommonBase.throwExceptionCommonBase(e);
		}catch (UnsupportedEncodingException e) {
			logger.error(e);
			throw ExceptionCommonBase.throwExceptionCommonBase(e);
		}
		return fileUrl;
	}

}
